# plugs/less.py
#
#

""" output cache """

__copyright__ = 'this file is in the public domain'

from ommbot.world import add_cmnd, add_example, add_alias

def handle_less(bot, ievent):
    try:
        if len(ievent.args) == 3:
            (who, index1, index2) = ievent.args
        elif len(ievent.args) == 2:
            who = ievent.nick
            (index1, index2) = ievent.args
        else:
            who = ievent.nick
            index1 = 0
            index2 = ievent.args[0]
        index1 = int(index1)
        index2 = int(index2)
    except IndexError:
        ievent.missing('[<who>] [<index1>] <index2>')
        return
    except ValueError:
        ievent.reply('i need integers as arguments')
        return
    txt = bot.less.get(who, index1, index2)
    if not txt:
        ievent.reply('no data available for %s %s %s' % \
(who, index1, index2))
        return
    ievent.reply(txt)

add_cmnd('less', handle_less, 'USER')
add_example('less', "less [<who>] [<index1>] <index2> .. get txt from bots \
             output cache", '1) less 0 2) less 0 2 3) less bart 1 0')

def handle_lesssize(bot, ievent):
    try:
        who = ievent.args[0]
    except IndexError:
        who = ievent.nick
    ievent.reply(bot.less.size(who))

add_cmnd('less-size', handle_lesssize, 'USER')
add_example('less-size', "show sizes of data in bot's ouput cache", \
            'less-size')

def handle_more(bot, ievent):
    try:
        who = ievent.args[0]
    except IndexError:
        who = ievent.nick
    what, size = bot.less.more(who, 0)
    if not what:
        ievent.reply('no more data available for %s' % who)
        return
    if size:
        bot.privmsg(ievent.printto, "%s (+%s)" % (what, size))
    else:
        ievent.reply(what)

add_cmnd('more', handle_more, 'USER')
add_example('more', 'return txt from output cache', 'more')
